<style lang="less">
@import "./login.less";
</style>

<template>
  <div class="login">
    <div class="login-con">
      <Card icon="log-in" title="欢迎登录" :bordered="false">
        <div class="form-con">
          <login-form @on-success-valid="handleSubmit"></login-form>
          <p class="login-tip">输入任意用户名和密码即可</p>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import LoginForm from '_c/login-form'
import { mapActions } from 'vuex'
import Main from '@/components/main'
import { dashboard } from '@/router/routers'

import {
  getTagsList,
  initRouterNode
  // setMenuListToLocalStorage
  // getMenuListFromLocalStorage
} from '@/libs/util'

export default {
  components: {
    LoginForm
  },
  methods: {
    ...mapActions([
      'handleLogin',
      'getUserInfo'
    ]),
    handleSubmit ({ userName, password }) {
      this.handleLogin({ userName, password }).then(menulist => {
        // console.log('handleLogin', menulist)
        let rowMenuList = menulist
        this.$store.dispatch('updateMenulist', rowMenuList).then(() => {
          if (menulist[0] && menulist[0]['NAME'] === 'home') {
            menulist.shift()
          }
          let routers = initRouterNode(menulist)
          // console.log(routers)

          const tagList = getTagsList(menulist)

          let rt = [{
            path: '/',
            name: '_home',
            redirect: '/home',
            component: Main,

            meta: {
              hideInMenu: true,
              hideInBread: true
            },
            children: [dashboard, ...routers]
          }]

          this.$router.addRoutes(rt)

          this.$store.commit('setTagsList', tagList)
          this.$store.commit('addRouters', routers)

          this.$router.push({ name: 'home' })
        })
      }).catch(err => {
        this.$store.commit('setSubmitDisabled', false)
        console.error(err)
      })
    }
  }
}
</script>

<style>
</style>
